﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.Content;
using System.Data.Linq.Mapping;
using System.Data.Linq;
using MethodWorx.Core.DomainModel;

namespace MethodWorx.CMS.Linq
{
    [Table(Name="dbo.ContentItem")]
    public class LinqContentItem : ContentItem, IPersistable
    {
        private PageProperty[] properties;
        private PageProperty[] publishedProperties;
        
        #region Proxy Property For Id
        [Column(UpdateCheck=UpdateCheck.Never, IsPrimaryKey=true)]
        public new Guid Id { get { return base.Id; } set { base.Id = value; } }
        #endregion
        #region Proxy Property For Name
        [Column(UpdateCheck=UpdateCheck.Never)]
        public new string Name { get { return base.Name; } set { base.Name = value; } }
        #endregion  
        #region Proxy Property For Page
        private EntityRef<LinqPage> proxyPage = new EntityRef<LinqPage>();
        
        [Column(UpdateCheck=UpdateCheck.Never)]
        public Guid PageId { get; set; }
        
        [Association(IsForeignKey=true, Name="Page", OtherKey="Id", ThisKey="PageId", Storage="proxyPage")]
        public LinqPage ProxyPage
        {
          get { return this.proxyPage.Entity; }
          set { this.proxyPage.Entity = value; }
        }
        
        public override Page Page
        {
          get { return this.ProxyPage; }
          set { this.ProxyPage = (LinqPage)value; }
        }
        #endregion
        #region Proxy Property For ContentTemplate
        private EntityRef<LinqContentTemplate> proxyContentTemplate = new EntityRef<LinqContentTemplate>();
        
        [Column(UpdateCheck=UpdateCheck.Never)]
        public Guid ContentTemplateId { get; set; }
        
        [Association(IsForeignKey=true, Name="ContentTemplate", OtherKey="Id", ThisKey="ContentTemplateId", Storage="proxyContentTemplate")]
        public LinqContentTemplate ProxyContentTemplate
        {
          get { return this.proxyContentTemplate.Entity; }
          set { this.proxyContentTemplate.Entity = value; }
        }
        
        public override ContentTemplate Template
        {
          get { return this.ProxyContentTemplate; }
          set { this.ProxyContentTemplate = (LinqContentTemplate)value; }
        }
        #endregion
        #region Proxy Property For Deleted
        [Column(UpdateCheck = UpdateCheck.Never)]
        public new bool Deleted { get { return base.Deleted; } set { base.Deleted = value; } }
        #endregion
        
        #region Proxy Property For Sequence
        [Column(UpdateCheck=UpdateCheck.Never)]
        public new int Sequence { get { return base.Sequence; } set { base.Sequence = value; } }
        #endregion
      

        [Column(UpdateCheck = UpdateCheck.Never, Name = "Properties")]
        public string PropertiesStorage { get; set; }

        public override PageProperty[] Properties
        {
            get
            {
                if (this.properties == null)
                {
                    this.properties = this.PropertiesStorage.Base64DeserializeUsingXml<PageProperty[]>() ?? new PageProperty[] { };
                }
                return this.properties;
            }
            set
            {
                this.properties = value;
                this.PropertiesStorage = this.properties.Base64SerializeUsingXml();
            }
        }

        [Column(UpdateCheck = UpdateCheck.Never, Name = "PublishedProperties")]
        public string PublishedPropertiesStorage { get; set; }

        public override PageProperty[] PublishedProperties
        {
            get
            {
                if (this.publishedProperties == null)
                {
                    this.publishedProperties = this.PublishedPropertiesStorage.Base64DeserializeUsingXml<PageProperty[]>() ?? new PageProperty[] { };
                }
                return this.publishedProperties;
            }
            set
            {
                this.publishedProperties = value;
                this.PublishedPropertiesStorage = this.publishedProperties.Base64SerializeUsingXml();
            }
        }


        #region Proxy Property For DateCreated
        [Column(UpdateCheck = UpdateCheck.Never)]
        public new DateTime DateCreated { get { return base.DateCreated; } set { base.DateCreated = value; } }
        #endregion

        #region Proxy Property For DatePublished
        [Column(UpdateCheck = UpdateCheck.Never)]
        public new DateTime? DatePublished { get { return base.DatePublished; } set { base.DatePublished = value; } }
        #endregion


        #region Proxy Property For Status
        [Column(UpdateCheck = UpdateCheck.Never)]
        public new ContentStatus Status { get { return base.Status; } set { base.Status = value; } }
        #endregion
    }
}
